<template>
  <div class="divBox">
    <el-card class="box-card">
      <div class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px">
            <el-form-item label="时间选择：" class="width100">
              <el-radio-group
                v-model="tableFrom.dateLimit"
                type="button"
                class="mr20"
                size="small"
                @change="selectChange(tableFrom.dateLimit)"
              >
                <el-radio-button v-for="(item, i) in fromList.fromTxt" :key="i" :value="item.val">{{
                  item.text
                }}</el-radio-button>
              </el-radio-group>
              <el-date-picker
                v-model="timeVal"
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
                size="small"
                type="daterange"
                placement="bottom-end"
                placeholder="自定义时间"
                style="width: 250px"
                @change="onchangeTime"
              />
            </el-form-item>
            <el-form-item label="结算状态：">
              <el-radio-group v-model="tableFrom.status" type="button" size="small" @change="getList()" clearable>
                <el-radio-button value="-1">全部</el-radio-button>
                <el-radio-button value="0">未结算</el-radio-button>
                <el-radio-button value="1">已结算</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="关键字：" class="width100">
              <el-input
                v-model="tableFrom.keywords"
                placeholder="账单标题"
                style="width: 390px;padding-right: 20px;"
                size="small"
                clearable
              >
              </el-input>
              <el-button type="primary" size="small" @click="getList()" >查询</el-button>
              <el-button type="warning" size="small" @click="buildBill()">生成账单</el-button>
            </el-form-item>
            
          </el-form>
        </div>
      </div>
    </el-card>
    <el-card class="box-card">
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
        highlight-current-row
      >
        <el-table-column prop="id" label="供应商编号" width="100" align="center" />
        <el-table-column prop="tenantName" label="供应商名称" min-width="180" align="center"/>
       
        <el-table-column prop="productOrderPayNum" label="发货订单数量" min-width="120" align="center"/>
        <el-table-column prop="productOrderPayFee" label="发货订单金额" min-width="120" align="center"/>
        <el-table-column prop="productOrderRefundNum" label="退款订单数量" min-width="120" align="center"/>
        <el-table-column prop="productOrderRefundFee" label="退款订单金额" min-width="120" align="center"/>
        <!-- <el-table-column prop="mark" label="账单标题" min-width="200"/> -->
        <el-table-column label="结算状态" min-width="100">
          <template #default="{row}">
            <span v-if="row.status === 0">
              未结算
            </span>
            <span v-else-if="row.status === 1">
              己结算
            </span>
            <span v-else>无效</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="统计日期" min-width="100"/>
        <!-- <el-table-column prop="createTime" label="创建时间" min-width="100" /> -->
        <el-table-column label="操作" min-width="250" fixed="right" align="center">
          <template #default="{row}">
            <el-button
                type="primary"
                size="small"
                @Click="handleDetailShow(row.date)">详情</el-button>
            <template v-if="row.status === 0">
              <el-button
                type="primary"
                size="small"
                @Click="handleConfirm(row.id)">确认结算</el-button
              >
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <el-dialog title="账单详情" v-model="tableDialogVisible" width="850px" :before-close="handleClose">
      <el-table
        :data="tableDetailData.data"
        style="width: 100%"
        size="small"
        :highlight-current-row="true"
        :header-cell-style="{ fontWeight: 'bold' }"
      >
        <el-table-column prop="id" label="ID" min-width="80"/>
        <el-table-column
          label="订单号"
          prop="orderId"
          min-width="120"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <!-- <el-table-column
          label="订单详情Id"
          prop="orderInfoId"
          min-width="120"
          :show-overflow-tooltip="true"
        >
        </el-table-column> -->
        <!-- <el-table-column
          label="商品ID"
          prop="productId"
          min-width="120"
          :show-overflow-tooltip="true"
        >
        </el-table-column> -->
        <el-table-column
          label="商品名称"
          prop="productName"
          min-width="120"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="价格"
          prop="cost"
          min-width="120"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="数量"
          prop="payNum"
          min-width="120"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
      
        <el-table-column label="交易类型" min-width="120" >
          <template #default="{row}">
            <div v-if="row.changeType == 'pay_success'">
              收款
            </div>
            <div v-if="row.changeType == 'refund_price'">
              退款
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="交易时间"
          prop="date"
          min-width="120"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { billListApi,billDetailListApi, billConfirmApi,billBuildApi} from '@/api/shopping/tenant';
export default {
  name: 'AccountsExtract',
  data() {
    return {
      timeVal: [],
      tableData: {
        data: [],
        total: 0,
      },
      tableDetailData: {
        data: [],
        total: 0,
      },
      listLoading: true,
      tableFrom: {
        status: -1,
        dateLimit: '',
        keywords: '',
        page: 1,
        limit: 20,
      },
      tableDetailFrom: {
        date: null,
        page: 1,
        limit: 9999,
      },
      fromList: this.$constants.fromList,
      applyId: null,
      tableDialogVisible: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 确认已结算
    handleConfirm(id) {
      this.$modalSure('结算吗?').then(() => {
        billConfirmApi(id).then(() => {
          ElMessage.success('操作成功');
          this.getList();
        });
      });
    },
    // 选择时间
    selectChange(tab) {
      this.timeVal = [];
      this.tableFrom.dateLimit = tab;
      this.tableFrom.page = 1;
      this.getList();
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.dateLimit = e ? this.timeVal.join(',') : '';
      this.tableFrom.page = 1;
      this.getList();
    },
    // 列表
    getList() {
      this.listLoading = true;
      billListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    getDetailList() {
      billDetailListApi(this.tableDetailFrom)
        .then((res) => {
          this.tableDetailData.data = res.data.list;
          this.tableDetailData.total = res.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    buildBill() { 
      billBuildApi(30)
        .then((res) => {
          ElMessage.success('生成账单成功');
          this.listLoading = false;
          this.getList();
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    handleDetailShow(date) {
      this.tableDetailFrom.date = date;
      this.getDetailList();
      this.tableDialogVisible = true;
    },
    handleClose() {
      this.tableDialogVisible = false;
      this.tableDetailData.data = [];
      this.tableDetailData.total = 0;
    },
  },
};
</script>

<style scoped>
.selWidth {
  width: 350px;
}
</style>
